package com.example.DataSource.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.DataSource.Mapper.CEMapper;
import com.example.DataSource.Service.CEService;
import com.example.DataSource.entity.consultant;
import com.example.DataSource.entity.consultantApplication;
import com.example.DataSource.entity.consultantEvaluate;
import com.example.DataSource.entity.member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CEServiceImpl extends ServiceImpl<CEMapper, consultantEvaluate> implements CEService {
    @Override
    @Transactional
    public IPage<consultantEvaluate> getCEPage(Page<consultantEvaluate> page) {
        QueryWrapper<consultantEvaluate> ceQueryWrapper = new QueryWrapper<>();
        IPage<consultantEvaluate> CEPage = this.page(page,ceQueryWrapper);
        List<consultantEvaluate> records = CEPage.getRecords();
//        需要根据会员编号去会员表查询会员昵称（单对单）
        for (consultantEvaluate ce :
                records) {
//            具体获取逻辑
            String memberName = getMemberName(ce.getVip_id());
            ce.setMemberName(memberName);
        }
//        需要根据预约咨询编号去预约咨询表CA查询预约记录，然后根据预约记录的咨询师ID查询咨询师的name（三表联查）
        for (consultantEvaluate ce :
                records) {
//            先根据预约编号获取查询预约记录
            int CAId = ce.getConsult_apt_id();
//            根据这个ID去查询CA表
            int consultantID = getConsultantId(CAId);
//            根据这个id去咨询师表获取咨询师名字
            String consultantName = getConsultantName(consultantID);
            ce.setConsultantName(consultantName);
        }
        return CEPage;
    }

    @Override
    @Transactional
    public IPage<consultantEvaluate> getCEPageSearch(Page<consultantEvaluate> page, String searchInfo) {
        QueryWrapper<consultantEvaluate> ceQueryWrapper = new QueryWrapper<>();
        ceQueryWrapper
                .like("content",searchInfo);
        IPage<consultantEvaluate> CEPage = this.page(page,ceQueryWrapper);
        List<consultantEvaluate> records = CEPage.getRecords();
//        需要根据会员编号去会员表查询会员昵称（单对单）
        for (consultantEvaluate ce :
                records) {
//            具体获取逻辑
            String memberName = getMemberName(ce.getVip_id());
            ce.setMemberName(memberName);
        }
//        需要根据预约咨询编号去预约咨询表CA查询预约记录，然后根据预约记录的咨询师ID查询咨询师的name（三表联查）
        for (consultantEvaluate ce :
                records) {
//            先根据预约编号获取查询预约记录
            int CAId = ce.getConsult_apt_id();
//            根据这个ID去查询CA表
            int consultantID = getConsultantId(CAId);
//            根据这个id去咨询师表获取咨询师名字
            String consultantName = getConsultantName(consultantID);
            ce.setConsultantName(consultantName);
        }
        return CEPage;
    }

    @Autowired
    private memberServiceImpl memberService;
    private String getMemberName(int vipId) {
//        根据会员Id查询会员昵称
        QueryWrapper<member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",vipId);
        return memberService.list(queryWrapper).get(0).getNickname();
    }

    @Autowired
    private CAServiceImpl caService;
    private int getConsultantId(int CAId){
        QueryWrapper<consultantApplication> caQueryWrapper = new QueryWrapper<>();
        caQueryWrapper.eq("id",CAId);
        return caService.list(caQueryWrapper).get(0).getConsultant_id();
    }

    @Autowired
    private consultantServiceImpl consultantService;
    private String getConsultantName(int caID){
        QueryWrapper<consultant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",caID);
        return consultantService.list(queryWrapper).get(0).getName();
    }
}
